<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>附件上传调试工具</title>
    <style>
        body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
        .section { margin: 20px 0; padding: 15px; border: 1px solid #ddd; border-radius: 5px; }
        .error { background: #fee; border-color: #fcc; }
        .success { background: #efe; border-color: #cfc; }
        .info { background: #eef; border-color: #ccf; }
        pre { background: #f5f5f5; padding: 10px; border-radius: 3px; overflow-x: auto; }
        button { padding: 8px 16px; margin: 5px; cursor: pointer; }
        input[type="file"] { margin: 10px 0; }
    </style>
</head>
<body>
    <h1>Halo附件上传调试工具</h1>
    
    <div class="section info">
        <h3>当前配置检查</h3>
        <p>工作目录: <code>~/.halo2</code></p>
        <p>附件存储: <code>~/.halo2/attachments/upload/</code></p>
        <p>用户中心API: <code>/apis/uc.api.storage.halo.run/v1alpha1/attachments/-/upload</code></p>
    </div>

    <div class="section">
        <h3>1. 测试用户中心附件上传API</h3>
        <input type="file" id="ucFileInput" accept="image/*" />
        <button onclick="testUcUpload()">测试UC上传</button>
        <div id="ucResult"></div>
    </div>

    <div class="section">
        <h3>2. 测试控制台附件上传API</h3>
        <input type="file" id="consoleFileInput" accept="image/*" />
        <button onclick="testConsoleUpload()">测试控制台上传</button>
        <div id="consoleResult"></div>
    </div>

    <div class="section">
        <h3>3. 检查存储策略配置</h3>
        <button onclick="checkSystemSettings()">检查系统设置</button>
        <div id="settingsResult"></div>
    </div>

    <div class="section">
        <h3>4. 检查附件存储策略</h3>
        <button onclick="checkPolicies()">检查存储策略</button>
        <div id="policiesResult"></div>
    </div>

    <script>
        // 测试用户中心附件上传
        async function testUcUpload() {
            const fileInput = document.getElementById('ucFileInput');
            const resultDiv = document.getElementById('ucResult');
            
            if (!fileInput.files.length) {
                resultDiv.innerHTML = '<div class="error">请选择一个文件</div>';
                return;
            }
            
            const file = fileInput.files[0];
            const formData = new FormData();
            formData.append('file', file);
            
            try {
                resultDiv.innerHTML = '<div class="info">正在上传...</div>';
                
                const response = await fetch('/apis/uc.api.storage.halo.run/v1alpha1/attachments/-/upload', {
                    method: 'POST',
                    body: formData,
                    credentials: 'include'
                });
                
                const result = await response.json();
                
                if (response.ok) {
                    resultDiv.innerHTML = `
                        <div class="success">
                            <h4>✅ UC上传成功</h4>
                            <pre>${JSON.stringify(result, null, 2)}</pre>
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `
                        <div class="error">
                            <h4>❌ UC上传失败 (${response.status})</h4>
                            <pre>${JSON.stringify(result, null, 2)}</pre>
                        </div>
                    `;
                }
            } catch (error) {
                resultDiv.innerHTML = `
                    <div class="error">
                        <h4>❌ UC上传异常</h4>
                        <pre>${error.message}</pre>
                    </div>
                `;
            }
        }
        
        // 测试控制台附件上传
        async function testConsoleUpload() {
            const fileInput = document.getElementById('consoleFileInput');
            const resultDiv = document.getElementById('consoleResult');
            
            if (!fileInput.files.length) {
                resultDiv.innerHTML = '<div class="error">请选择一个文件</div>';
                return;
            }
            
            const file = fileInput.files[0];
            const formData = new FormData();
            formData.append('file', file);
            formData.append('policyName', 'default-policy');
            
            try {
                resultDiv.innerHTML = '<div class="info">正在上传...</div>';
                
                const response = await fetch('/apis/api.console.halo.run/v1alpha1/attachments/upload', {
                    method: 'POST',
                    body: formData,
                    credentials: 'include'
                });
                
                const result = await response.json();
                
                if (response.ok) {
                    resultDiv.innerHTML = `
                        <div class="success">
                            <h4>✅ 控制台上传成功</h4>
                            <pre>${JSON.stringify(result, null, 2)}</pre>
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `
                        <div class="error">
                            <h4>❌ 控制台上传失败 (${response.status})</h4>
                            <pre>${JSON.stringify(result, null, 2)}</pre>
                        </div>
                    `;
                }
            } catch (error) {
                resultDiv.innerHTML = `
                    <div class="error">
                        <h4>❌ 控制台上传异常</h4>
                        <pre>${error.message}</pre>
                    </div>
                `;
            }
        }
        
        // 检查系统设置
        async function checkSystemSettings() {
            const resultDiv = document.getElementById('settingsResult');
            
            try {
                resultDiv.innerHTML = '<div class="info">正在检查系统设置...</div>';
                
                const response = await fetch('/apis/v1alpha1/configmaps/system', {
                    credentials: 'include'
                });
                
                if (response.ok) {
                    const result = await response.json();
                    const userData = result.data?.user ? JSON.parse(result.data.user) : {};
                    
                    resultDiv.innerHTML = `
                        <div class="success">
                            <h4>✅ 系统设置</h4>
                            <p>用户中心附件策略: <code>${userData.ucAttachmentPolicy || '未设置'}</code></p>
                            <p>头像存储策略: <code>${userData.avatarPolicy || '未设置'}</code></p>
                            <details>
                                <summary>完整配置</summary>
                                <pre>${JSON.stringify(userData, null, 2)}</pre>
                            </details>
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `
                        <div class="error">
                            <h4>❌ 获取系统设置失败 (${response.status})</h4>
                        </div>
                    `;
                }
            } catch (error) {
                resultDiv.innerHTML = `
                    <div class="error">
                        <h4>❌ 检查系统设置异常</h4>
                        <pre>${error.message}</pre>
                    </div>
                `;
            }
        }
        
        // 检查存储策略
        async function checkPolicies() {
            const resultDiv = document.getElementById('policiesResult');
            
            try {
                resultDiv.innerHTML = '<div class="info">正在检查存储策略...</div>';
                
                const response = await fetch('/apis/storage.halo.run/v1alpha1/policies', {
                    credentials: 'include'
                });
                
                if (response.ok) {
                    const result = await response.json();
                    
                    resultDiv.innerHTML = `
                        <div class="success">
                            <h4>✅ 存储策略列表</h4>
                            ${result.items.map(policy => `
                                <div style="margin: 10px 0; padding: 10px; border: 1px solid #ddd;">
                                    <strong>${policy.metadata.name}</strong> - ${policy.spec.displayName}
                                    <br>模板: ${policy.spec.templateName}
                                    <br>配置: ${policy.spec.configMapName}
                                </div>
                            `).join('')}
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `
                        <div class="error">
                            <h4>❌ 获取存储策略失败 (${response.status})</h4>
                        </div>
                    `;
                }
            } catch (error) {
                resultDiv.innerHTML = `
                    <div class="error">
                        <h4>❌ 检查存储策略异常</h4>
                        <pre>${error.message}</pre>
                    </div>
                `;
            }
        }
        
        // 页面加载时自动检查配置
        window.onload = function() {
            checkSystemSettings();
            checkPolicies();
        };
    </script>
</body>
</html>